MacOS X Developer Preview 4 Release Notes Copyright © 2000 by Apple Computer, Inc. All Rights Reserved.
The Java APIs for the core Cocoa frameworks rest upon the foundation of Apple's Java/Objective-C integration technology (also known as the "Java Bridge"). This technology allows you to access the full potential of the Cocoa frameworks using Java code and without having to write any Objective-C code. The bridge even allows you to subclass Objective-C objects in Java; in addition, using the bridging tools, you can wrap your own Objective-C code to make it accessible to Java applications.
The following new APIs added to AppKit and Foundation between DP3 and DP4 are not exported in the Java APIs. This is a bug and will be addressed for the next update:
All other new API has Java counterparts.
We intend to rename com.apple.yellow to com.apple.cocoa in the next update.
The Java VM was updated between DP1 and DP2. This was found to cause some incompatibilities:
Some of the APIs added between DP1 and DP2 do not yet appear in the Java frameworks. For example, the new tooltip methods on NSView and NSMatrix.
NSTypesetter, recently made public, is not available in the Java APIs.
com.apple.yellow.application
and com.apple.yellow.foundation.
In
addition, the same class and interface names are used in
the Objective-C and Java APIs; thus the Objective-C
prefixes are retained (NSApplication instead of
Application, EOQualifier instead of Qualifier, etc). Note
that the package names are likely to be changed to refer
to "Cocoa" for the final release. replaceCharactersInRange:withAttributedString:
and replaceCharactersInRange:withString:
are
now both represented as replaceCharactersInRange()
in the Java APIs. /System/Developer/Java/Conversion/AlphaToYellow
.
Be sure to make a copy of your source directory before
you apply the conversion, as the script works in place.
Once you have run the scripts, trying to compile your
program should pinpoint the remaining areas where
conversion is still needed.loadNibNamed()
,
which is added by the Application Kit, is available in
NSApplication in Java.com.apple.yellow.foundation._private
should be considered private and not used in your
applications. Similarly, com.apple.yellow.application._NSObsoleteMenuItemProtocol
is also private and should not be used.[NSView initWithFrame:rect
], you
use NSView(rect)
to create views. This of
course is the most natural way for Java, although
sometimes the set of constructors might seem confusing.
For instance, NSAttributedString provides the following
constructors: NSAttributedString(String);
(from
a string)
NSAttributedString(String, NSDictionary);
(from
a string and attributes)
NSAttributedString(URL, NSMutableDictionary);
(from
the specified file, returning document properties)
NSAttributedString(NSData, NSMutableDictionary);
(from
the specified file contents, returning document
properties)
NSAttributedString(NSFileWrapper,
NSMutableDictionary);
(from the specified file
contents, returning document properties)
NSAttributedString(NSData, URL,
NSMutableDictionary);
(from the specified HTML
file contents and base URL, returning doc properties)
In some cases, multiple initialization methods have
been mapped to a single constructor in Java with multiple
arguments; for instance, NSImage's initByReferencingFile:
and initWithContentsOfFile:
both map to NSImage(String,
boolean)
, with the boolean indicating whether to
reference (YES) or not (NO). Note that use of URLs to
represent files also eliminates some of these cases.
Finally, in other cases, static methods are used
instead of constructors. This not only reduces the
confusion associated with having many constructors, it
also allows substitution of objects if necessary. NSColor
is an example of this, with methods such as colorWithCalibratedRGB()
,
colorWithDeviceCMYK()
, blackColor()
,
scrollBarColor()
, and so on.
retain
. If an object is created
in Java, and passed across the bridge into Objective-C,
and it is not retained by anyone on the Objective-C side,
the object's proxy will get released, causing the weak
references on the Objective-C side to be invalidated,
thereby causing invalid memory access. One way to work
around this is to ensure that such objects are referenced
strongly (that is, retained) in the Objective-C side; you
can assure this by putting such objects into an NSArray.
TextEdit does this with its Document objects. Examples:
-(NSDictionary)attributesAtIndex:(int)index
effectiveRange:(NSRangePointer)range
maps to
NSDictionary attributesAtIndex(int index, NSMutableRange
range)
void NSRectFillList(const NSRect *rects, int
count)
maps to
void fillRectList(NSRect[] rects)
Along the same lines, mutable versions of other classes (for example, NSDictionary) are used in APIs which pass these classes by reference in order to get a value back from the caller.
setObjectForKey()
method in NSDictionary in Java checks to see whether the
object is a instance of a class corresponding to these
structs. These are converted to an NSValue before being
stored in the dictionary. In addition objectForKey()
performs the reverse mapping. This allows the storage of
these kinds of values in NSDictionaries to happen
naturally from both Java and Objective-C. Note that the
NSNumber to Number conversion already happens in all
cases. Examples of this include NSScreen's deviceDescription(),
which returns a dictionary, and
NSAttributedString's RTFFromRange()
, which
takes a dictionary as an argument.
Two limitations of this feature are:
valueWithRect:
instead
of valueWithBytes:objCType:
);
otherwise the automatic conversion won't take
place.Note that in general morphing is the right thing to do: You want to communicate strings in Java using Java's built-in java.lang.String class. Like NSString, this class stores a string of Unicode characters and provides API to deal with them. Thus most of the Cocoa API in Java still takes or returns String, which is automatically converted to and from an NSString.
NSImage(URL)
in addition to the constructors taking file name as a
String. Similarly, you can use the NSOpenPanel method URLs()
to get back an array of URLs that represent the files
selected by the user. Although these methods currently
work only with URLs representing local files, they will
soon work with other types of URLs. In addition,
additional APIs that take or return URLs will be added
throughout the frameworks where appropriate.fileAttributes(),
setFileAttributes(), pathFromURL(), URLWithPath(),
and searchPathForDirectoriesInDomains())
cover functionality available in Objective-C through
functions or unexposed classes.push()
and pop()
are exposed in Java. They should rarely be needed.